#pragma strict

private var secretKey="mySecretKey"; // Edit this value and make sure it's the same as the one stored on the server php file.
private var addScoreUrl="http://localhost/battlezone3d/addscore.php?"; //be sure to add a ? to your url
private var highscoreUrl="http://localhost/battlezone3d/display.php";

//This connects to a server side php script that will add the name and score to a MySQL DB.
// Supply it with a string representing the players name and the players score.
function postScore(name:String, score : int) {
	Debug.Log("Saving: name: " + name + " " + "score: " + score + " in the Database.");

    var hash = Md5.Md5Sum(name + score + secretKey);
    var highscore_url = addScoreUrl + "name=" + WWW.EscapeURL(name) + "&score=" + score + "&hash=" + hash;
 
    // Post the URL to the site and create a download object to get the result.
    var hs_post = WWW(highscore_url);
    yield hs_post; // Wait until the download is done
    
    if(hs_post.error) {
        print("There was an error posting the high score: " + hs_post.error);
    }
}
 
//Get the scores from the MySQL DB.
function getScores(){
	var scriptsGObj = GameObject.FindGameObjectWithTag("Scripts");
	
    var hs_get = WWW(highscoreUrl);
    yield hs_get;
 
    if(hs_get.error) {
    	print("There was an error getting the high score: " + hs_get.error);
    } else {
    	Debug.Log("hs_get.text: " + hs_get.text);
        scriptsGObj.GetComponent(GUISurface).setScoreboard(hs_get.text); // this is a GUIText that will display the scores in game.
    }
}